package com.customComboBox
{
	import flash.events.Event;
	import flash.text.TextLineMetrics;
	
	import mx.controls.Button;
	import mx.core.IFactory;
	import mx.core.UIComponent;
	import mx.events.DropdownEvent;
 
    [Event(name="change", type="flash.events.Event")]
    [Event(name="textChanged", type="flash.events.Event")]
    [Event(name="placementChanged", type="flash.events.Event")]
    

    public class testComp extends UIComponent 
    {	
    	//public var dataProvider:XMLList;
    	public var dataProvider:Array;
    	public var itemRenderer : IFactory;

    	
    	private var combo_mc:customComboBox
        private var mode_mc:Button;
        
        
        public function testComp() {
            super();
        }

        override protected function createChildren():void {
            super.createChildren();
                  
            if (!combo_mc)
            {
                combo_mc = new customComboBox();
                combo_mc.editable = false;
                combo_mc.labelField="@name"
                
                combo_mc.addEventListener(DropdownEvent.OPEN, onOpen);
                combo_mc.addEventListener(DropdownEvent.CLOSE, onClose);
                addChild(combo_mc);
                callLater(setComboData);                            
            }
                    
            if (!mode_mc)
            {   
            	mode_mc = new Button();
                mode_mc.label = "search";
                mode_mc.addEventListener("click", handleClickEvent);
                addChild(mode_mc);
                mode_mc.visible = false;
            }
        }
        
        private function setComboData():void
        {
        	combo_mc.dataProvider = dataProvider;
        	//combo_mc.rowCount = dataProvider.length();
            combo_mc.prompt = "테마를 선택하세요.";
			combo_mc.rowCount = 5;        	
        	combo_mc.itemRenderer = itemRenderer;
        }
                
        override protected function commitProperties():void {
            super.commitProperties();
            /*
            if (bTextChanged) {
                bTextChanged = false;
                combo_mc.text = _text;
                invalidateDisplayList();
            }*/
        }       

        override protected function measure():void {
            super.measure();
            
            measuredWidth = combo_mc.measuredWidth;
            
            if(combo_mc.dropdown)
            { 
            	measuredHeight = combo_mc.dropdown.height + mode_mc.height;
            }
            else
            {
            	measuredHeight = combo_mc.height + mode_mc.height;
            }
        }
 
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
            super.updateDisplayList(unscaledWidth, unscaledHeight);         
            
            var usableWidth:Number = unscaledWidth - 8;
            var usableHeight:Number = unscaledHeight - 8;
            
            // Calculate the size of the Button control based on its text.
            var lineMetrics:TextLineMetrics = measureText(mode_mc.label);
            // Add a 10 pixel border area around the text.
            var buttonWidth:Number = lineMetrics.width + 10;
            var buttonHeight:Number = lineMetrics.height + 10;
            mode_mc.setActualSize(buttonWidth, buttonHeight);       
            combo_mc.setActualSize(buttonWidth+50, buttonHeight);
            // Calculate the size of the text
            // Allow for a 5 pixel gap between the Button 
            // and the TextArea controls. 
            //var textWidth:Number = usableWidth - buttonWidth - 5;
            if(combo_mc.dropdown)
            	var textHeight:Number = usableHeight + combo_mc.dropdown.height;
            else
            	textHeight = usableHeight;
            //combo_mc.setActualSize(textWidth, textHeight);
            
            
            combo_mc.move(4, 4);

			if(combo_mc.dropdown)
            	mode_mc.move(4, 4 + combo_mc.height + combo_mc.dropdown.height + 5);
            else
            	mode_mc.move(4, 4 + combo_mc.height + 5);
            
            
            // Draw a simple border around the child components.
            //graphics.lineStyle(1, 0x000000, 1.0);
            //graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);         
        }
        
        private function onOpen(eventObj:DropdownEvent):void {
        	mode_mc.visible = true;
        	updateDisplayList(width,height);
			//dispatchEvent(new Event("change"));
        }
        
        private function onClose(eventObj:DropdownEvent):void {
        	mode_mc.visible = false;
        	updateDisplayList(width,height);
			//dispatchEvent(new Event("change"));
        }
        
        private function handleClickEvent(eventObj:Event):void {
        	combo_mc.isClose = true;
            combo_mc.close();
        }
    }
}
